/**
 * 内容为ifrmae的对话框,建议为不同类型的对话框分别实例化此实例,这样title/width/height就不用修改了
 * left top title width height showToolbar
 * @param title
 * @param width
 * @param height
 * @constructor
 */
function DI(settings){
    this.x = settings.left || 0;
    this.y = settings.top || 0;
    this.title = settings.title || "对话框";
    this.width = settings.width || 360;
    this.height = settings.height || 0;
    this.showToolbar = true;
    if(typeof settings.showToolbar === "boolean" && settings.showToolbar === false){
        this.showToolbar = false;
    }
    var id_suffix = Math.ceil(Math.random()*1000000);
    this.did = "_di_"+id_suffix;
    var dHtml = "<div id=\""+this.did+"\" class=\"dialog\" style=\"display:none;z-index:99999;\">" +
        "<span class=\"dialogTitle\"><span>"+this.title+"</span><a href=\"javascript:void(0);\"></a></span>" +
        "<div data-tag=\"frame_div\" style=\"overflow: hidden;\"><iframe name='di_frame' src='/none.html' style='width:100%;height:100%;border:0;overflow: auto;' /></div>";
    if(this.showToolbar){
        dHtml+="<div class=\"toolbar2\"><button class='ok'>确定</button><button class='cancel'>取消</button></div>";
    }
    dHtml+="</div>";
    this.$d = $(dHtml);
    this.$d.appendTo("body");
    var _instance = this;
    //为对话框增加移动事件
    this.$d.find(".dialogTitle").mousedown(function (e) { //e鼠标事件
        var offset = _instance.$d.offset(); //DIV在页面的位置
        var x = e.pageX - offset.left; //获得鼠标指针离DIV元素左边界的距离
        var y = e.pageY - offset.top; //获得鼠标指针离DIV元素上边界的距离
        $(document).bind("mousemove", function (ev)//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件
        {
            _instance.$d.stop(); //加上这个之后
            var _x = ev.pageX - x; //获得X轴方向移动的值
            var _y = ev.pageY - y; //获得Y轴方向移动的值
            _instance.$d.animate({ left: _x + "px", top: _y + "px" }, 10);
        });
    });
    $(document).mouseup(function () {
        $(this).unbind("mousemove");
    });
    this.$d.find(".dialogTitle a").click(function(){
        _instance.close();
    });
    this.$d.find(".cancel").click(function(){
        _instance.close();
    });
    var css = {"width":this.width+"px"};
    this.$d.css(css);
    if(this.height>0){
        css = {};
        css.height = this.height+"px";
        this.$d.find("div[data-tag='frame_div']").css(css);
    }
    this.resetXY(null);
};
DI.prototype.show = function(url,data,okHandler){
    if(typeof url !== "string" && url.length==0){
        alert("系统对话框显示时必须指定url参数来获取对话框数据,请确定.");
        return;
    }
    var iframe = this.$d.find("iframe[name='di_frame']");
    if(this.showToolbar){
        //给确定按钮附加事件
        var _instance = this;
        var okBtn = this.$d.find(".ok");
        if(typeof okHandler === "function"){
            okBtn.css("display","");
            okBtn.unbind();
            okBtn.click(function(){
                var ret = iframe[0].contentWindow.doOk();
                if(ret){
                    var r = okHandler(ret);
                    if(typeof r == "undefined" || r){
                        _instance.close();
                    }
                }
            });
        }else{
            okBtn.css("display","none");
        }
    }
    //从url中加载数据,并装载到内容区域中
    var _url = Http.getUrl(url,data);
    if(iframe.attr("src") != _url){
        iframe.attr("src",_url);
    }
    //重新定位并显示对话框
    this.$d.css({"display":"block","left": this.x+"px","top": this.y+"px"});
};
DI.prototype.close = function(){
    this.$d.hide();
};
DI.prototype.resetXY = function(sender){
    var xy = $(sender).offset();
    var ww = $(document).width();
    var dw = this.$d.width();
    if(xy && xy.left){
        this.x= xy.left-dw/2;
        if (this.x<0) this.x=10;
        if ((this.x+dw)>ww) this.x=ww-dw-20;
        this.y = xy.top;
    }else{
        //居中显示
        if(this.x == 0){
            this.x = (ww-dw)/2;
        }
        if(this.y == 0){
            this.y = 200;
        }
    }
};